<?php
  class BuildFile {
     
  function deleteDirectory($dir) {
    if (!file_exists($dir)) return true;
    if (!is_dir($dir) || is_link($dir)) return unlink($dir);
        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..') continue;
            if (!deleteDirectory($dir . "/" . $item)) {
                chmod($dir . "/" . $item, 0777);
                if (!deleteDirectory($dir . "/" . $item)) return false;
            };
        }
        return rmdir($dir);
    } 
    
   function createFileRefcode($fileRefcode,$refcode)
   {
         //$myFile = "refcode.txt";
         $fh = fopen($fileRefcode, 'w') or die("can't open file");
         $stringData = $refcode."\n";
         fwrite($fh, $stringData);
         fclose($fh);
   }
   function changeExtensionJadToJar($fileJad){
       $arr =array();
       $arr=explode(".",$fileJad);
       $arr[count($arr)-1]="jar";
       $fileJar = implode(".",$arr);
       return $fileJar;
   }
   function extractRefcode($fileZip,$fileExtract,$extractTo){
        $zip = new ZipArchive;
        $res = $zip->open($fileZip);
        if ($res === TRUE) {
            $zip->extractTo($extractTo, array($fileExtract));
            $zip->close();
        } else {
        }
   }

   function addFileRefcode($fileIpa,$fileRefcode,$fileRefcodeDes){
            $zip = new ZipArchive;
            if ($zip->open($fileIpa) === TRUE) {
                $zip->addFile($fileRefcode, $fileRefcodeDes);
                $zip->close();
            } else {
            }
   }        
           
  function extractFileZip($fileZip,$extractTo){
    $zip = new ZipArchive;
    if ($zip->open($fileZip) === true) {
        for($i = 0; $i < $zip->numFiles; $i++) {
            $zip->extractTo($extractTo, array($zip->getNameIndex($i)));
        }
        $zip->close();
    }
  }

   function downloadFile($url_file,$fileName,$fileExtension){
           header("Pragma: public");
            if($fileExtension=="jad"){
                header("Content-type: text/vnd.sun.j2me.app-descriptor");    
            } else if($fileExtension=="jar"){
                header("Content-type: application/java-archive;");    
            } else if($fileExtension=="apk"){
                header("Content-type: application/vnd.android.package-archive");        
            }

            header('Content-Disposition: attachment; filename="' . $fileName . "." . $fileExtension . '"');                
            ob_clean();
            flush();
            $content = Common::cUrl($url_file);
            echo $content;
            exit();
   }  
   
   function changeLineFile($file,$line,$line_content){
        $handle = @fopen($file, "r+");
        $contentFileNew="";
          $i = 1;
        if ($handle) {
            while (($buffer = fgets($handle, 4096)) !== false) {
                if($i==$line){
                 $contentFileNew .= $line_content."\n"; 
                }
                else{
                    $contentFileNew .= $buffer;
                }
                $i++;
            }

            if (!feof($handle)) {
                echo "Error: unexpected fgets() fail\n";
            }
           @file_put_contents($file, $contentFileNew);
           @fclose($handle);
        }
   }  
  
  
   function buildFileJava($fileJarSource,$folderJarDes,$appId,$refcode,$filename,$username,$type,$url_file){
        if (!file_exists($folderJarDes)) {
            Common::recursive_mkdir($folderJarDes, 0755);
         }
         switch($appId){  
            case 6 :
            case 7 :
            case 14 :
            case 15 :
            case 16 :
            case 17 :
            case 18 :
            case 19 :
            case 21 :
            {
                @copy($fileJarSource,$folderJarDes.$filename);
                BuildFile::extractRefcode($fileJarSource,"META-INF/MANIFEST.MF",$folderJarDes);
                BuildFile::changeLineFile($folderJarDes."META-INF/MANIFEST.MF",6,"refCode: ".$refcode);
                BuildFile::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/MANIFEST.MF","META-INF/MANIFEST.MF")    ;
                @unlink($folderJarDes."META-INF/MANIFEST.MF");
                
                if(strpos($filename, ".jad")===false){
                    $fileJad = basename($filename,".jar").".jad";
                    $fileJabSource = str_replace(".jar",".jad",$fileJarSource);
                    @copy($fileJabSource,$folderJarDes.$fileJad);
                    BuildFile::changeLineFile($folderJarDes.$fileJad,3,"MIDlet-Jar-URL: ".$url_file);  
                }else{
                    $url_file = str_replace(".jad",".jar",$url_file);
                    BuildFile::changeLineFile($folderJarDes.$filename,3,"MIDlet-Jar-URL: ".$url_file);  
                }
                 break;
            }
            
            
         }
    }
    function buildFileJad($fileJarSource,$folderJarDes,$appId,$refcode,$filename,$username,$type){
        if (!file_exists($folderJarDes)) {
            Common::recursive_mkdir($folderJarDes, 0755);
         }
         switch($appId){  
            case 2 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildFile::changeExtensionJadToJar($filename);
                BuildFile::changeLineFile($folderJarDes.$filename,9,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildFile::changeLineFile($folderJarDes.$filename,4,"RefCode: ".$refcode);
                break;
            }
             case 4 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildFile::changeExtensionJadToJar($filename);
                BuildFile::changeLineFile($folderJarDes.$filename,5,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildFile::changeLineFile($folderJarDes.$filename,15,"RefCode: ".$refcode);
                break;
            }
            case 8 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildFile::changeExtensionJadToJar($filename);
                BuildFile::changeLineFile($folderJarDes.$filename,3,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildFile::changeLineFile($folderJarDes.$filename,13,"Channel: 236-".$refcode);
                break;
            }
            case 5 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildFile::changeExtensionJadToJar($filename);
                BuildFile::changeLineFile($folderJarDes.$filename,5,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildFile::changeLineFile($folderJarDes.$filename,14,"RefCode: ".$refcode);
                break;
            }
            case 7 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildFile::changeExtensionJadToJar($filename);
                BuildFile::changeLineFile($folderJarDes.$filename,6,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildFile::changeLineFile($folderJarDes.$filename,16,"RefCode: ".$refcode);
                break;
            }
         }
    }
    
     function buildAppIPA($fileSource,$folderDes,$appId,$filename,$fileRefcodeDes,$username,$refcode){
         if (!file_exists($folderDes)) {
            Common::recursive_mkdir($folderDes, 0755);
         }   
         switch($appId){  
            default:
            {
                @copy($fileSource,$folderDes.$filename);
                $fileRefcode = $folderDes."refcode.txt";
                BuildFile::createFileRefcode($fileRefcode,$refcode) ;
                BuildFile::addFileRefcode($folderDes.$filename,$fileRefcode,$fileRefcodeDes);  
                @unlink($fileRefcode);
             
                /* Build file plist*/
                $basename = basename($filename,".ipa");
                $arr=array();
                $arr = explode("/",$fileSource);
                $arr[count($arr)-1]= $basename.".plist";
                $fileSourcePlist = implode("/",$arr);
                @copy($fileSourcePlist,$folderDes.$basename.".plist");
                
               $lineContent ="<string>http://mtop.vn/upload/user/".$username."/app/".$filename."</string>";
               $contentFileNew = BuildFile::changeLineFile($folderDes.$basename.".plist",14,$lineContent);
              break;
            }
         }
    }
    
    
    function buildAppAPK($fileSource,$folderDes,$appId,$filename,$username,$refcode){
        
        if (!file_exists($folderDes)) {
           Common::recursive_mkdir($folderDes, 0755);
        }   
         switch($appId){  
            case 2 :
            { 
                @copy($fileSource,$folderDes.$filename);
                BuildFile::extractRefcode($fileSource,"jad.MF",$folderDes);
                BuildFile::changeLineFile($folderDes."jad.MF",11,"Refcode: ".$refcode);
                BuildFile::addFileRefcode($folderDes.$filename,$folderDes."jad.MF","jad.MF");
                @unlink($folderDes."jad.MF");
                break;
            }
            default  :
            { 
                @copy($fileSource,$folderDes.$filename);
                $fileRefcode = $folderDes."refcode.txt";
                $fileRefcodeDes = 'assets/refcode.txt';
                BuildFile::createFileRefcode($fileRefcode,$refcode) ;
                BuildFile::addFileRefcode($folderDes.$filename,$fileRefcode,$fileRefcodeDes)    ;
                @unlink($fileRefcode);
                break;
            }
            
         }
        
        //asign apk
        
        $pathApkTool = "/home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/signapk.jar";
        $pathApkTool1 = "/home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.x509.pem";
        $pathApkTool2 = "/home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.pk8";
        
        /*$pathApkTool = "D:/wamp/www/appstore/kichhoat_apk/EasyApkDisassembler/SYSTEM/signapk.jar";
        $pathApkTool1 = "D:/wamp/www/appstore/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.x509.pem";
        $pathApkTool2 = "D:/wamp/www/appstore/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.pk8";*/
        
        $file1 = $folderDes.$filename; 
        $file2= $folderDes."asinged_".$filename; 
        $command ="java -jar ".$pathApkTool." ".$pathApkTool1. " ".$pathApkTool2." ".$file1." ".$file2."   ";
        @exec($command, $output, $response);
        @rename($file2,$file1);
    }
  }
?>
